<template>
  <div>
    <NavBar :title="title" />
    <div class="content-top">
      <van-cell-group>
        <van-cell @click="onBindPhone" title="手机号" :value="phone != '' ? phone : '未绑定'" is-link size="large" />
      </van-cell-group>
      <van-cell-group style="margin-top: 10px">
        <!-- <van-cell title="腾讯QQ" value="未绑定" is-link size="large" /> -->
        <van-cell @click="bindApple( isBindApple ? 0 : 1)" title="AppleID" :value="isBindApple?'已绑定':'未绑定'" is-link size="large" />
        <van-cell @click="bindWX(isBindWx ? 0 : 1)" title="微信" :value="isBindWx ? '已绑定' : '未绑定'" is-link size="large" />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
import NavBar from "@/components/NavBar.vue";
import { Cell, CellGroup, Dialog, Toast } from "vant";
import {
  PersonLoginedGetBindInfo,
  PersonUnBindWX,
  PersonBindWX,
} from "@/util/http";
import { returnToPhoneWithData } from "@/util/phone";
export default {
  name: "AccountIndex",
  components: {
    NavBar,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
  },
  props: ["title"],
  activated() {
    this.onLoad();
  },
  // 定义属性
  data() {
    return {
      phone: "",
      wx: "",
      appleID: '',
			isBindWx:false,
			isBindApple:false,
    };
  },
  created() {
    window.doBindWX = this.doBindWX;
		window.doBindApple  = this.doBindApple;
  },
  // 方法集合
  methods: {
    onLoad() {
      this.$api.get('/PersonAccount/GetBindInfo').then(res => {
        this.phone = res.mobileNum;
        this.wx = res.wxID;
        this.appleID = res.appleID;
				this.isBindWx = res.isBindWx,
				this.isBindApple = res.isBindApple
      })
    },
    //绑定手机
    onBindPhone() {
      this.$router.push({ name: "person-setting-account-bindphone" });
    },
    //绑定、解绑微信 type（0解绑  1绑定）
    bindWX(type) {
      if (type === 1) {
        //绑定
        returnToPhoneWithData({ action: "person_bind_wx", });
      } else {
        //解绑
        Dialog.confirm({ message: "确认解绑微信？", confirmButtonText: "解绑", }).then(() => {
          this.$api.post('/PersonAccount/UnBind',1).then(res => {
            Toast(res.msg);
            if (res.success) {
              this.onLoad();
            }
          })
        })
      }
    },
    bindApple(type) {
      // console.log(type);
      if (type === 1) {
        //绑定
        returnToPhoneWithData({ action: "person_bind_apple", });
      } else {
        //解绑
        Dialog.confirm({ message: "确认解绑AppleID？", confirmButtonText: "解绑", }).then(() => {
          this.$api.post('/PersonAccount/UnBind',2).then(res => {
            Toast(res.msg);
            if (res.success) {
              this.onLoad();
            }
          })
        })
      }
    },
    doBindWX(wxUnionID, wxOpenID) {
      this.$api.post('/PersonAccount/BindWechat',wxUnionID).then(res => {
        Toast(res.msg);
        if (res.success) {
          this.onLoad();
        }
      })
    },
		doBindApple(appleID) {
      this.$api.post('/PersonAccount/BindApple',appleID).then(res => {
        Toast(res.msg);
        if (res.success) {
          this.onLoad();
        }
      })
    },
  },
};
</script>

<style lang="less">
@import "vant/lib/dialog/index.less";
@import "@/assets/css/common.less";
</style>
